package com.example.android.softkeyboard;



public class TextHelper {
	
	public static String sentenceSeparators;
	public static String wordSeparators;
	public static String numbers;
	public static String noPrespaceSeparators;
	public static String postspaceSeparators;
	
	public static String[] words = new String[4];
	public static String letters;
	
	public static boolean isEmpty(String s) {
		boolean empty = false;
		if (s == null || s.length() == 0) {
			empty = true;
		}
		return empty;
	}
	
	public static void setSeparators(String sentenceSep, String wordSep, String num, String noSpace, String postSpace) {
		sentenceSeparators = sentenceSep;
		wordSeparators = wordSep;
		numbers = num;
		noPrespaceSeparators = noSpace;
		postspaceSeparators = postSpace;
	}
	
	public static String[] processText(CharSequence text) {
		letters = "";
		for (int i = 0; i < words.length; i++) {
        	words[i] = "";
        }
    	
		if (text != null && text.length() > 0) {
			String[] sentences = text.toString().split(sentenceSeparators);
			String currentSentence = "";

			int textLength = text.length();
			if (textLength > 0) {
				CharSequence lastCharacter = text.subSequence(textLength - 1, textLength);
				if (sentences.length > 0 && !sentenceSeparators.contains(lastCharacter)) {
					currentSentence = sentences[sentences.length - 1];
				}
			}

			String[] currentWords = currentSentence.split(wordSeparators);
			int wordAmount = currentWords.length;

			int characterAmount = currentSentence.length();
			if (characterAmount > 0) {
				CharSequence lastCharacter = currentSentence.subSequence(characterAmount - 1, characterAmount);
				if (!wordSeparators.contains(lastCharacter)) {
					String lastWord = "";
					if (wordAmount > 0) {
						lastWord = currentWords[wordAmount-1];
					}                        
					letters = lastWord;
				}
			}

			wordAmount -= 1;

			if (wordAmount > 0) {
				int firstRelevantWord = 0;
				int relevantWords = 0;
				for (int i = wordAmount-1; i >= 0; i--) {
					if (TextHelper.isEmpty(currentWords[i]) == false) {
						relevantWords++;
						firstRelevantWord = i;
					}
					if (relevantWords == 4) {
						break;
					}
				}
				if (relevantWords > 0) {
					int counter = 0;
					for (int i = firstRelevantWord; i < wordAmount; i++) {
						if (TextHelper.isEmpty(currentWords[i])== false) {
							words[counter] = currentWords[i];
							counter++;
						}
					}
				}
			}
		}
		String[] result = new String[5];
		System.arraycopy(words, 0, result, 0, words.length);
		result[4] = letters;
		
		return result;
    }
	
	public static String toString(String[] str) {
		String result = "";
		for (String s : str) {
			result += s + ", ";
		}
		return result;
	}
	
	public static String getSeparators() {
		return wordSeparators + sentenceSeparators;
	}
	
    public static boolean isSeparator(int code) {
        String separators = getSeparators();
        return separators.contains(String.valueOf((char)code));
    }
    
    public static boolean isNumber(int code) {
    	return numbers.contains(String.valueOf((char)code));
    }
    
    public static boolean isNoPrespaceSeparator(String character) {
    	return noPrespaceSeparators.contains(character);
    }
    
    public static boolean isPostspaceSeparator(String character) {
    	return postspaceSeparators.contains(character);
    }
	
}
